<?php
session_start();
require_once ("../src/db/connectdatabase.php");
require_once ("../model/PartNumberToMoney.php");

function loaddataCompareRate($amount, $vaytoida, $thunhap, $hinhthuctraluong, $isbank, $contry, $typerate, $tttrchan, $sqltemp, $orderby) {
	$con = new ConnectDB();
	
	$sql = "SELECT c.idcom, c.bankid, b.bankname, b.banklogo, c.idloanmaxi, c.amountloanmin, c.amountloanmax, c.interrestrate, c.interrestrate2, c.phitattoan, c.conditionbasic FROM compareloanspersional AS c, banks AS b WHERE c.bankid=b.bankid AND c.amountloanmin <= ".$amount." AND c.amountloanmax >= ".$amount." AND c.salarypermounth >= ".$thunhap." AND c.idloanmaxi LIKE '%".$vaytoida."%' AND c.hinhthuctraluong LIKE '%".$hinhthuctraluong."%' AND c.isbankphi LIKE '%".$isbank."%' AND c.country LIKE '%".$contry."%' AND c.typerate LIKE '%".$typerate."%' AND c.tttrchan LIKE '%".$tttrchan."%' ".$sqltemp." AND c.isuse=1 ".$orderby;
	return $con -> getvalueString($sql);
}

if(isset($_POST['act'])) {
	if($_POST['act'] == "load") {
		if(isset($_POST['amounre']) && isset($_POST['start']) && isset($_POST['salary']) && isset($_POST['ratemax']) && isset($_POST['typepaysalary']) && isset($_POST['bankre']) && isset($_POST['contry']) && isset($_POST['tyrate']) && isset($_POST['ttrchan'])) {
			$amount = $_POST['amounre'];
			$timestart = $_POST['start'];
			$thunhap = $_POST['salary'];
			$vaytoida = $_POST['ratemax'];
			$hinhthuctraluong = $_POST['typepaysalary'];
			$isbank = $_POST['bankre'];
			$contry = $_POST['contry'];
			$typerate = $_POST['tyrate'];
			$tttrchan = $_POST['ttrchan'];
			
			$sqltemp = "";
			$orderby = "";
			if($timestart != "") {
				$sqltemp = " AND c.timeloan >= '".$timestart."'";
				$orderby = " ORDER BY c.timeloan DESC";
			}
			
			$arrresult = loaddataCompareRate($amount, $vaytoida, $thunhap, $hinhthuctraluong, $isbank, $contry, $typerate, $tttrchan, $sqltemp, $orderby);
			if(count($arrresult) > 0) {
				for($i = 0; $i < count($arrresult); $i++) {
					$stridloanmaxitemp = $arrresult[$i][4];// cot khoan vay toi da
					$arridloanmaxi = explode(',', $stridloanmaxitemp);
					$nametemp = "";
					for($j = 0; $j < count($arridloanmaxi); $j++) {
						if($arridloanmaxi[$j] == "1") $nametemp .= "Dưới 6 lần thu nhập. "; else $nametemp .= "Trên 6 lần thu nhập. ";
					}
					$arrresult[$i][4] = $nametemp;
				}
				echo json_encode($arrresult);
			} else {
				echo json_encode("null");
			}
			if(isset($_SESSION['arrcomparerate'])) {
				unset($_SESSION['arrcomparerate']);
			}
		}
	} else if($_POST['act'] == "sscom") {
		if(isset($_POST['pr']) && isset($_POST['idre'])) {
			$arrcompare = array();
			if(isset($_SESSION['arrcomparerate'])) {
				$arrcompare = $_SESSION['arrcomparerate'];
			}
			$acc = $_POST['pr'];
			$idcom = $_POST['idre'];
			if($acc == "add") {
				$arrcompare.array_push($arrcompare, $idcom);
			} else if($acc == "del") {
				$arrcompare = array_diff($arrcompare, array($idcom));
			}
			$_SESSION['arrcomparerate'] = $arrcompare;
			echo json_encode("ok");
		}
	}
}
?>